<?php

require_once BaseUtil::getModulePath("dbgrid") . '/DBGrid.php';
require_once BaseUtil::getModulePath("dbgrid") . '/DBGridConstant.php';
require_once BaseUtil::getModulePath("base") . '/services/BaseService.php';
require_once BaseUtil::getModulePath("config") . '/models/ConfigDao.php';

class ConfigService extends BaseService {

    private $configDao;

    public function __construct() {
        parent::__construct();
        $this->configDao = new ConfigDao();
        $this->configDao->setDb($this->db);
    }

    public function getConfigs($view,$form) {
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/config/config");
        $dbGrid->addValueFilter("is_delete",0);
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"conf_key",$this->_translate->translate("conf_key"));
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"conf_value",$this->_translate->translate("conf_value"));
        $dbGrid->addFetchTable("configs","*");
        $dbGrid->addFetchColumn("conf_key",$this->_translate->translate("conf_key"),"edit");
        $dbGrid->addFetchColumn("conf_value",$this->_translate->translate("conf_value"));
        $dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
        //module action
        $dbGrid->addAAModuleAction("config","edit",$this->_translate->translate("editBtn"),"edit");
        $dbGrid->addAAModuleAction("config","delete",$this->_translate->translate("delBtn"),"deletesome");
        $dbGrid->addAAModuleAction("config","add",$this->_translate->translate ("addBtn" ),"add");
        $dbGrid->setPageTitle($this->_translate->translate("searchTitle"));
        return $dbGrid->render();
    }

    public function addConfig($form) {
        $config = array();
        $config["conf_key"] = $form->getValue("conf_key");
        $config["conf_value"] = $form->getValue("conf_value");
        $this->configDao->insertData($config);
    }

    public function editConfig($form) {
        $config = array();
        $config["conf_value"] = $form->getValue("verification_email");
        $this->configDao->updateConfig($config,"verification_email");
        $config["conf_value"]= $form->getValue("template_name");
        $this->configDao->updateConfig($config,"template_name");
        $config["conf_value"]= $form->getValue("shipping_rate_1");
        $this->configDao->updateConfig($config,"shipping_rate_1");
        $config["conf_value"]= $form->getValue("shipping_rate_2");
        $this->configDao->updateConfig($config,"shipping_rate_2");
        $config["conf_value"]= $form->getValue("shipping_rate_3");
        $this->configDao->updateConfig($config,"shipping_rate_3");
        $config["conf_value"]= $form->getValue("shipping_rate_4");
        $this->configDao->updateConfig($config,"shipping_rate_4");
        $config["conf_value"]= $form->getValue("shipping_rate_5");
        $this->configDao->updateConfig($config,"shipping_rate_5");
    }

    public function getConfigById($id) {
        return $this->configDao->getByCol("id",$id);
    }

    public function deleteConfigs($ids) {
        foreach($ids as $id) {
            if(!empty($id)) {
                $this->configDao->deleteConfig($id);
            }
        }
    }

    public function getConfigByKey() {
        $config = array();
        $results =$this->configDao->getConfig();
        foreach($results as $result) {
            $key = $result['conf_key'];
            $value = $result['conf_value'];
            $config[$key] = $value;
        }
        return $config;
    }
}